package cn.coldleaf.wiki.utils;

import com.baomidou.mybatisplus.core.toolkit.AES;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * md5工具类
 * @datetime 2023/10/15 上午11:41
 * @user cold
 **/
@Slf4j
public class SHAUtil {
    private static final String ALGORITHM= "SHA-256";
    
    /**
     * sha-256加密
     * @param binaryData 待加密数据
     * @datetime 2023-10-15 12:45:49
     * @user cold
     **/
    private static String encode(byte[] binaryData) {
        try {
            MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
            byte[] digests = digest.digest(binaryData);
            return StringUtil.bytes2HexString(digests);
        } catch (NoSuchAlgorithmException e) {
            log.error("md5加密失败",e);
        }
        return null;
    }
    /**
     * sha-256加密
     * @param bytes 待加密字节码字符串
     * @datetime 2023-10-15 11:44:15
     * @user cold
     **/
    public static String encrypt(byte [] bytes){
        if (bytes == null || bytes.length == 0){
            return null;
        }
        return encode(bytes);
    }
    
    /**
     * sha-256加密
     * @param str 待加密字符串
     * @datetime 2023-10-15 11:45:47
     * @user cold
     **/
    public static String encrypt(String str){
        if (StringUtil.isBlank(str)){
            return null;
        }
        return encrypt(str.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 验证签名
     * @param data 待验签数据
     * @param sign 签名
     * @datetime 2023-10-15 11:50:53
     * @user cold
     **/
    public static boolean verify(byte [] data, String sign){
        if (data == null || data.length == 0 || StringUtil.isBlank(sign)){
            return false;
        }
        String ret = encrypt(data);
        return sign.equals(ret);
    }

    /**
     * 验证签名
     * @param data 待验签数据
     * @param sign 签名
     * @datetime 2023-10-15 11:50:53
     * @user cold
     **/
    public static boolean verify(String data, String sign){
        if (StringUtil.isBlank(data) || StringUtil.isBlank(sign)){
            return false;
        }
        byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
        return verify(bytes,sign);
    }

    public static void main(String[] args) {
        System.out.println("sha:"+SHAUtil.encrypt("xxxxxxxxx"));
        System.out.println(SHAUtil.encrypt("xxxxxxxxx").length());
        String key = "8d2d31b316803711";
        System.out.println(AES.encrypt("jdbc:mysql://10.0.0.6:3306/coldleaf_wiki?useUnicode=true&charactorEncoding=UTF-8&UseAffectedRows=true&serverTimezone=Asia/Shanghai",key));
        System.out.println(AES.encrypt("coldleaf_wiki",key));
        System.out.println(AES.encrypt("coldleaf_wiki@123",key));
        System.out.println("xxxxxxxxxxxxxx".hashCode());//1735239936
        key = AES.generateRandomKey();
        key = "17574f471696de92";
        System.out.println(key);
        System.out.println(AES.encrypt("jdbc:mysql://localhost:3306/coldleaf_wiki_pro?useUnicode=true&charactorEncoding=UTF-8&UseAffectedRows=true&serverTimezone=Asia/Shanghai",key));
        System.out.println(AES.encrypt("coldleaf_wiki_pro",key));
        System.out.println(AES.encrypt("coldleaf_wiki_pro@123",key));
        System.out.println(AES.decrypt("YoLg11Wr9ODDUummXscP7q8m4+D4TiU6xGmWvNeKtt0C1AjkWWl8XvItHNgp8kdfe7LK5vHLuTOOCf8Nb3TSmrHTYcqaEM8xKQISfos2Pbpox5U8p46Z6u1Hr/YGp2yRgqxaZDmVta4vuUN7u+MkGsEVo3SjnRwRCD8a5VJFfqYwyeUa1pkJMUGMP3QmdqgT",key));
    }
}
